package leetcode;

public class SearchInRotatedSortedArray {

    public static void main(String[] args) {
        SearchInRotatedSortedArray s = new SearchInRotatedSortedArray();
        System.out.println(s.search(new int[]{5, 1, 3}, 3));
    }

    public int search(int[] nums, int target) {
        int l = 0, r = nums.length, middle = 0;
        if (r == 0) {
            return -1;
        }
        if (r == 1) {
            return nums[0] == target ? 0 : -1;
        }

        r --;

        while (l <= r){
            middle = (l + r) / 2;
            if(nums[middle] == target) return middle;
            if(nums[0] <= nums[middle]){
                if(target >= nums[0] && target < nums[middle]){
                    r = middle - 1;
                }else{
                    l = middle + 1;
                }
            }else{
                if(target > nums[middle] && target <= nums[nums.length - 1]){
                    l = middle + 1;
                }else{
                    r = middle -1;
                }
            }
        }
        return -1;
    }

}
